package com.example.lotterysystem.common.utils;

import com.example.lotterysystem.common.errorcode.ServiceErrorCodeConstants;
import com.example.lotterysystem.common.exception.ServiceException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

/**
 * 发送邮件
 */
@Component
@RequiredArgsConstructor
public class MailUtil {
	/**
	 * 邮件发送者
	 */
	@Value("${spring.mail.username}")
	private String MAIL_SENDER;
 
	/**
	 * 注入QQ发送邮件的bean
	 */
	private final JavaMailSender javaMailSender;

	/**
	 * 发送邮件
	 *
	 * @param recipient 接收者
	 * @param subject 主题
	 * @param content 内容
	 */
	public void sendMail(String recipient,String subject,String content) {

		try {
			SimpleMailMessage mailMessage = new SimpleMailMessage();
			//发送者
			mailMessage.setFrom(MAIL_SENDER);
			//接收者
			mailMessage.setTo(recipient);
			//邮件标题
			mailMessage.setSubject(subject);
			//邮件内容
			mailMessage.setText(content);
			//发送邮箱
			javaMailSender.send(mailMessage);

		} catch (Exception e) {

			throw new ServiceException(ServiceErrorCodeConstants.VERIFICATION_CODE_SEND_ERROR);
		}
	}

}
 
